/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.image;

import com.kurvykurvy.core.ui.Slide;
import java.awt.Graphics2D;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DragSourceMotionListener;
import java.awt.image.BufferedImage;

/**
 *
 * @author HY
 */
public class ImageSlide
        extends Slide
        implements DragGestureListener, DragSourceMotionListener, DragSourceListener
{
    LoadedImage image;

    private ImageSlide()
    {
        super();
        createRenderingHints();
    }

    public ImageSlide( LoadedImage img )
    {
        this();
        this.image = img;
        initSlide();
    }

    public ImageSlide( BufferedImage img )
    {
        this();
        this.image = new LoadedImage( img );
        initSlide();
    }

    protected void initSlide()
    {
        if ( this.image != null && this.image.getSourceImage() != null )
        {
            BufferedImage img = this.image.getSourceImage();
            aspectRatio = (double) img.getWidth() / (double) img.getHeight();
        }
    }

    protected void renderOffscreen()
    {
        buffer = new BufferedImage( getItemWidth(), getItemHeight(), BufferedImage.TYPE_INT_ARGB );

        int left = OUTER_MARGIN.left;
        int top = OUTER_MARGIN.top;
        int width = ITEM_WIDTH + INNER_MARGIN.left + INNER_MARGIN.right;
        int height = ITEM_HEIGHT + INNER_MARGIN.top + INNER_MARGIN.bottom;

        Graphics2D g2 = buffer.createGraphics();
        g2.setRenderingHints( hints );

        g2.drawImage( getPhotoBackground(),
        OUTER_MARGIN.left - 3 - SHADOW_SIZE / 2, OUTER_MARGIN.top - 3,
        getPhotoBackground().getWidth() + 6, getPhotoBackground().getHeight() + 6, null );

        g2.fillRect( left, top, width, height );

        left += INNER_MARGIN.left;
        top += INNER_MARGIN.top;
        width = ITEM_WIDTH;
        height = (int) (width / aspectRatio);

        g2.setClip( left, top, width, CLIP_HEIGHT );
        if ( aspectRatio < 1.0 )
        {
            top -= (int) ((height - CLIP_HEIGHT) / 2.0);
        }


        thumbnail = JavaImageUtils.createThumbnail( this.image.getImage(), width );
        g2.drawImage( thumbnail, left, top, null );
        g2.dispose();
    }


    public LoadedImage getImage()
    {
        return image;
    }

    public void setImage( LoadedImage image )
    {
        this.image = image;
    }
    
    @Override
    public Transferable getTransferable()
    {
        return this.image;
    }

    @Override
    public String getShortDescription()
    {
        return this.image.getFilename();
    }
}
